<?php


namespace App\Http\Controllers\WeChatService;


class WeChatServiceUtils
{
    // 根据模板选择id选择传递的消息
    public static function CreateModelMessage($open_id, $model_id, $model_data=[]) {
        $postData = [];
        $postData['touser'] = $open_id;
        $postData['template_id'] = WeChatConfig::ModelList[$model_id];
        $postData['topcolor'] = "#FF0000";
        $postData['data'] = $model_data;
        return $postData;
    }

    // 给用户发送订单变动状态通知
    public static function SendOrderChange($contract_no, $openID) {
        $access_token = WeChatServiceSDK::GetAccessTokenFromRedis();
        if ($access_token) {
            $model_data = [
                "contract"=>[
                    "value"=>$contract_no ?? '',
                    "color"=>"#173177"
                ]
            ];
            $postData = self::CreateModelMessage($openID, '8', $model_data);
            $postUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
            WeChatServiceSDK::post($postData, $postUrl);
            return true;
        } else {
            return false;
        }
    }

    // 给客户发送订单生效通知
    public static function SendOrderEffect($contract_no, $openID) {
        $access_token = WeChatServiceSDK::GetAccessTokenFromRedis();
        if ($access_token) {
            $model_data = [
                "contract"=>[
                    "value"=>$contract_no ?? '',
                    "color"=>"#173177"
                ]
            ];
            $postData = self::CreateModelMessage($openID, '7', $model_data);
            $postUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
            WeChatServiceSDK::post($postData, $postUrl);
            return true;
        } else {
            return false;
        }
    }

    // 给客户发送订单回款通知
    public static function SendOrderCollection($contract_no, $openID, $date, $money) {
        $access_token = WeChatServiceSDK::GetAccessTokenFromRedis();
        if ($access_token) {
            $model_data = [
                "contractNo"=>[
                    "value"=>$contract_no ?? '',
                    "color"=>"#173177"
                ],
                "date"=>[
                    "value"=>$date ?? '',
                    "color"=>"#173177"
                ],
                "money"=>[
                    "value"=>$money ?? '',
                    "color"=>"#173177"
                ]
            ];
            $postData = self::CreateModelMessage($openID, '1', $model_data);
            $postUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
            WeChatServiceSDK::post($postData, $postUrl);
            return true;
        } else {
            return false;
        }
    }

}
